<?php
/*
 * @Create By: Vscode
 * @Author: falcon
 * @Date: 2020-10-11 20:51:59
 * 更新货架信息
 */

namespace application\timer;
use application\depend\Applog;
use application\depend\Dbiov;
use application\model\PdoModel;

class UpdateShelf{

    public function run(){
        $getTimerJob = $this->getJob();
        if(empty($getTimerJob)) return;  // 为空 或 报错 戒不执行
        $code = $getTimerJob[0]['wh_code'];
        $skuList = $this->getWarehouseAllSku($code);
        if(empty($skuList)){// 如果sku 列表为空，代表该仓库的SKU 可能被清空了。那就不需要再执行该任务了。 直接改状态； 不然会一致卡在这里。
            $id = $getTimerJob[0]['id'];
            $this->updateJobStatus($id);
            return; //返回 等待执行下一个任务;
        }

        // 循环 sku 列表， 查找每个仓库 sku 的货位信息
        $codelist = $this->getWarehouseCode();

        $refreshData = $this->seeking($skuList,$codelist);

        $updateResult = $this->refreshVirhouseShelf($refreshData);

        if($updateResult){ // 更新成功了， 就更新任务状态
            $id = $getTimerJob[0]['id'];
            $this->updateJobStatus($id);
        }
        $pdo = Dbiov::getInstance();
        $pdo->closeDb();// 最后再释放一次
    }// func

    public function getJob(){
        $sql = "SELECT id,wh_code FROM shelf_refresh_scan_timer WHERE status=1 limit 1"; // 1.未处理 2.已处理
        $result = PdoModel::selectData($sql,[],'获取任务');
        return $result;
    }

    public function getWarehouseAllSku($code){
        $warehouse = 'warehouse'.$code;
        $sql = "SELECT distinct sku FROM $warehouse";
        $result = PdoModel::selectData($sql,[],'获取'.$code.'号仓库所有SKU种类');
        return $result;
    }// func

    public function getWarehouseCode(){
        $sql = "SELECT wh_code,type FROM warehouse_manage WHERE status=1";
        $result = PdoModel::selectData($sql,[],'获取所有仓库编码');
        return $result;
    }// func

    public function seeking($skuList,$codelist){
        // 数组 
        $arr = [];

        foreach($skuList as $v){
            $sku = $v['sku'];
            foreach($codelist as $vv){
                $warehouse = 'warehouse' . $vv['wh_code'];
                $sql = "SELECT shelf FROM $warehouse WHERE sku='$sku' limit 1";
                $result = PdoModel::selectData($sql,[],'获取SKU对应'.$warehouse.'货架名称');
                if(!empty($result) && is_array($result)){
                    if(empty($arr[$sku]) || (!empty($arr[$sku]) && !in_array($result[0]['shelf'],$arr[$sku]))){
                        $arr[$sku][] = $result[0]['shelf'];
                    }
                }
            } // foreach
        }// foreach

        //整理数据： sku -> xxx,xxx,xxx,
        $arr2 = [];
        $tmp = '';
        foreach($arr as $k=>$v){
            $tmp = '';
            if(is_array($v)){
                foreach($v as $vv){
                    $tmp .= $vv . ',';
                }// foreach
            }// if
            if(!empty($tmp)) $tmp = rtrim($tmp,',');
            $arr2[$k] = $tmp;
            
        }// foreach

        return $arr2;
    }// func

    public function refreshVirhouseShelf($refreshData){
        $pdo = Dbiov::getInstance();
        $pdo->trans();
        foreach($refreshData as $k=>$v){
            $skuType = getSKUtype($k);
            if(0 == $skuType) continue; //一般不会到这里
            $table = 'product';
            if(2 == $skuType) $table = 'product_acc';
            $sql = "UPDATE $table SET product_location = '$v' WHERE sku='$k'";
            $res = PdoModel::selectData($sql,[],'更新虚拟仓'.$table.'货位');
            if(false === $res){
                $pdo->rollback();
                $pdo->closeDb();
                Applog::infoLog($refreshData);
                return false;
            }// if
        }// foreach
        $pdo->commit();
        $pdo->closeDb();
        return true;
    }// func

    public function updateJobStatus($id){
        $finishedTime=date('Y-m-d H:i:s');
        $sql = "UPDATE shelf_refresh_scan_timer SET status=2,finished_time='$finishedTime' WHERE id=$id";
        PdoModel::selectData($sql,[],'更新任务状态');
    }
}// class